package com.yk._06_Concurrent的三个工具类;

import lombok.SneakyThrows;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class _01CountDownLanch {
    public static void main(String[] args) throws InterruptedException {

        //比如我想同步文章,
        // 1.需要替换文章里面的图片到自己服务器,
        // 2.需要提取关键字,
        // 3.需要提取摘要,
        // 4.三面三个步骤完成后,进入mysql
        long l1 = System.currentTimeMillis();

        ArticleService articleService = new ArticleService();
        CountDownLatch countDownLatch = new CountDownLatch(3);

        new Thread(() -> {
            articleService.replaceImg();
            countDownLatch.countDown();
        },"A").start();

        new Thread(() -> {
            articleService.keywords();
            countDownLatch.countDown();
        },"B").start();
        new Thread(() -> {
            articleService.summary();
            countDownLatch.countDown();
        },"C").start();
        //等到上面三个都完成了我就可以入库mysql了
        //
        countDownLatch.await();

        articleService.save();
        long l2 = System.currentTimeMillis();
        System.out.println("本来需要 花费 ." + (5 + 3 + 1)*1000 + "现在花费时间" + (l2 - l1));
    }
}

class ArticleService {


    @SneakyThrows
    public void replaceImg() {
        TimeUnit.SECONDS.sleep(5);
        System.out.println("替换文章图片完成,替换掉外网的图片地址");
    }
    @SneakyThrows
    public void keywords() {
        TimeUnit.SECONDS.sleep(1);
        System.out.println("提取关键字完成,并且设置到文章 keyword字段里面");
    }
    @SneakyThrows
    public void summary() {
        TimeUnit.SECONDS.sleep(3);
        System.out.println("提取摘要完成 ,并且设置到文章 tag字段里面");
    }

    @SneakyThrows
    public void save() {
        System.out.println("入库mysql");
    }
}
